<template>
  <div class="fillcontain">
    <!-- 具体配置 -->
    <div style="display:flex;">
      <!-- 左侧树 -->
      <asstnt-stndrd-items @queryStandItemTableData="queryStandItemTableData" ref="projectLibrary" itmeRootName="rule">
      </asstnt-stndrd-items>
      <!-- 右侧配置信息 -->
      <div class="right-main" style="height: 720px;">
        <!-- 操作按钮 -->
        <el-row class="btn-list">
          <el-col :span="24" class="pb15 pt5">
            <el-button type="danger" size="mini" style="margin-left: 10px" plain @click="ruleAddClick">新增 </el-button>
            <el-button type="danger" size="mini" style="margin-left: 10px" plain @click="ruleSaveClick">保存</el-button>
            <!-- <el-button type="danger" size="mini" style="margin-left: 10px" plain @click="importExcel()"
              :loading="importLoading">导出Excel
            </el-button> -->
          </el-col>
        </el-row>
        <!-- 体检分级规则组件 -->
        <physicalExamination v-if="ruleType == '1'" ref="physicalExamination" :logicDic="logicDic"
          :itemsCode="commonProperty" :itemsName="itemsName" :itemID="itemID">
        </physicalExamination>
        <!-- 特殊判断规则组件 -->
        <specialRule v-else-if="ruleType == '2'" ref="specialRule" :logicDic="logicDic" :itemsCode="commonProperty" :itemID="itemID">
        </specialRule>
        <!-- 核保结论规则 -->
        <underwritingConclusionsRule v-else-if="ruleType == '3'" ref="underwritingConclusionsRule" :logicDic="logicDic" :itemID="itemID">
        </underwritingConclusionsRule>
      </div>
    </div>
  </div>
</template>

<script>

import specialRule from "../../../component/specialRule";
import physicalExamination from "../../../component/physicalExamination";
import underwritingConclusionsRule from "../../../component/underwritingConclusionsRule";
import asstntStndrdItems from '../../../../common/asstntStndrdItems.vue';

import constant from '@/utils/constant';

import {
  // 体检风险规则
  importPhysicalRiskRuleApi,
  importHealthRiskRuleApi,
  // 特殊评分项
  importSpecialScoreRuleApi,
  // 核保评点项
  importUnderwritingEvaluationRuleApi,
  // 字典
  getNameDictProjectApi,
  getOwnDictProjectApi,
} from "@/api/medicalLibrary/examProjectLibrary";
import {
  // 体检分级规则API
  saveAusAsstntScorRuleInfoeApi,
  // 特殊评分规则API
  saveAsstntScorSpecialRuleApi,
  // 核保结论规则API
  saveUnderwritingConclusionsRuleApi,
}
  from "@/api/medicalLibrary/underwritingRule";

export default {
  directives: {
  },
  components: {
    asstntStndrdItems,
    specialRule,
    physicalExamination,
    underwritingConclusionsRule,
  },
  data() {
    return {
      itemID: "",
      itemsName: "",
      commonProperty: "",
      ruleGroupDic: [], //规则组枚举值
      projectCategoryDic: [],  //体检项对象.项目大类
      projectNameDic: [], //体检项对象.项目名称
      logicDic: [], //逻辑符

      // 点击哪个疾病下的规则
      ruleType: '', //点击疾病下的规则类型 1、体检风险项规则、2健康风险项规则、3特殊评分规则、4核保结论规则
      diseaseRuleId: '',  //点击的规则类型id
      top: 0,
      left: 0,
      // 点击保存、导入按钮时显示加载中
      importLoading: false,

      // select框分页
      page: {
        pageNum: 1,
        pageSize: 100,
        parameter: ''
      },

    };
  },
  created() {
    // 获取逻辑符的下拉值
    this.getNameDictProject()
  },
  methods: {
    queryStandItemTableData(id, itemsCode, isLock, currentRow) {
      this.commonProperty = itemsCode;
      this.itemID = id;
      this.ruleType = currentRow.ruleType
      this.itemsName = currentRow.itemsName
      let refName = this.ruleType == '1' ? 'physicalExamination' : this.ruleType == '2' ? 'specialRule' : 'underwritingConclusionsRule'
      setTimeout(() => {
        this.$refs[refName].getRuleDataList(this.commonProperty)
      }, 0)
    },
    ruleAddClick() {
      if(this.commonProperty == undefined || this.commonProperty =='' || this.commonProperty == null){
          this.$message.closeAll()
            this.$message.warning('请选择体检项目!')
            throw new Error('')
      }
      let refName = this.ruleType == '1' ? 'physicalExamination' : this.ruleType == '2' ? 'specialRule' : 'underwritingConclusionsRule'
      setTimeout(() => {
        this.$refs[refName].handleAdd()
      }, 0)
    },
    // 点击保存按钮
    ruleSaveClick() {
      if(this.commonProperty == undefined || this.commonProperty =='' || this.commonProperty == null){
          this.$message.closeAll()
            this.$message.warning('请选择体检项目!')
            throw new Error('')
      }
      let url = this.ruleType == '1' ? saveAusAsstntScorRuleInfoeApi : this.ruleType == '2' ? saveAsstntScorSpecialRuleApi : saveUnderwritingConclusionsRuleApi
      let refName = this.ruleType == '1' ? 'physicalExamination' : this.ruleType == '2' ? 'specialRule' : 'underwritingConclusionsRule'
      var flag = this.$refs[refName].checkSaveInfo();
      if (!flag) {
        return
      }
      let dataList = this.$refs[refName].tableData;
      let params = {
        dataList: dataList,
        itemsCode: this.commonProperty,
      }
      url(params).then(res => {
        if (res.resultCode == constant.SUCCESS) {
          this.$refs[refName].getRuleDataList(this.commonProperty);
          this.$message.success('保存成功')
          setTimeout(() => {
          }, 500)
        }
      }).catch(err => {
      });
    },
    // 查询逻辑符
    getNameDictProject() {
      getOwnDictProjectApi({ code: 'ruleType' }).then(res => {
        if (res.resultCode == constant.SUCCESS) {
          this.ruleGroupDic = res.data || []
          this.ruleGroupDic.forEach(item => {
            item.value = item.remark
            item.label = item.dictValue
          })
        }
      })
      getNameDictProjectApi({ name: '逻辑符' }).then(res => {
        if (res.resultCode == constant.SUCCESS) {
          this.logicDic = res.data
        }
      })
    },

    // 导出execl
    importExcel() {
      let url = this.ruleType == '1' ? importPhysicalRiskRuleApi : this.ruleType == '2' ? importHealthRiskRuleApi :
        this.ruleType == '3' ? importSpecialScoreRuleApi : importUnderwritingEvaluationRuleApi
      this.importLoading = true

      url({ diseaseRuleId: this.diseaseRuleId, versionCode: this.versionId }).then(res => {
        this.importLoading = false
        let disposition = res.headers['content-disposition'],
          filename = decodeURI(disposition.split(';')[1].split('=')[1]).replace(/^(\s|")+|(\s|")+$/g, '')
        filename = filename.split('+-+')[1]
        let blob = new Blob([res.data], { type: `${res.data.type};charset=utf-8` })
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
          window.navigator.msSaveOrOpenBlob(blob, filename);
        }
        let downloadElement = document.createElement('a');
        let href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = filename; //下载后文件名
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象
      }).catch(err => {
        this.importLoading = false
      });
    },
  },
  watch: {
  },
}
</script>

<style lang="less" scoped>
.right_box {
  width: calc(100% - 350px);
  /* border: 1px solid #e3dddd; */
}

.fillcontain {
  padding-bottom: 0;

  .fastQuery {
    padding: 15px 20px 10px 20px;
    background: #fff;
    border-bottom: 1px solid #e1e1e1;

    .fastQuery-title {
      font-size: 16px;
    }
  }

  .content {
    padding-top: 15px;
    /* height: calc(100vh - 262px);
    min-height: 450px; */
    height: 570px;
    margin: 0 10px;
    padding-bottom: 10px;
    display: flex;
  }

  .left-tree {
    width: 300px;
    margin-right: 10px;
    border: 1px solid #e1e1e1;

    .tree-filter {
      padding: 20px 20px 0 20px;
    }

    .tree-content {
      margin: 0 10px;
    }

    .expanded {
      font-size: 12px;
      border: 1px solid #808080;
      color: #808080;
    }

    .node-label {
      display: inline-block;
      padding: 10px 0;
      width: 200px;
    }

    .node-active {
      color: #83a1f7;
    }

    .node-ellips {
      margin-left: 14px;
      display: inline-block;
      max-width: 152px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .custom-tree-node {
      display: flex;
      align-items: center;
      flex: 1;
    }
  }

  .right-main {
    flex: 1;
    overflow: auto;

    .btn-list {
      position: relative;
      background: #f8f8f8;
      padding: 10px 0;
      margin-bottom: 10px;

      p {
        position: absolute;
        right: 10px;
        top: 16px;
        color: #f76767;
      }
    }
  }
}

.contextMenu {
  margin: 0;
  border: 1px solid #dadce0;
  background: #fff;
  z-index: 3000;
  position: fixed;
  list-style-type: none;
  padding: 5px 0;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 400;
  color: #333;
  box-shadow: 2px 2px 3px 0 rgb(0 0 0 / 30%);
  text-align: center;
  width: 120px;
}

.contextMenu li {
  cursor: pointer;
  margin: 8px;
}

.contextMenu li:hover {
  color: #68a3fc;
}

/deep/ .pb15 {
  padding: 0px !important;
}

/deep/ .el-tree {
  background: #fff !important;
  height: calc(100% - 100px);
  overflow: auto;
  margin-top: 20px !important;
  padding-top: 0px;
}

/deep/ .el-tree-node__expand-icon {
  display: none;
}

/deep/ .el-tree-node__content {
  height: 30px;
}

/deep/ .el-tree-node {
  position: relative;
}

/deep/ .left-tree .tree-content>.el-tree-node:before,
/deep/ .left-tree .tree-content>.el-tree-node>.el-tree-node__children>.el-tree-node:before,
/deep/ .left-tree .tree-content>.el-tree-node>.el-tree-node__children>.el-tree-node .el-tree-node__children:before {
  content: " ";
  border-left: 1px dashed #4386c6;
  position: absolute;
  bottom: 0px;
  top: 30px;
  left: 8px;
  width: 1px;
  z-index: 98;
}

/deep/ .left-tree .tree-content>.el-tree-node>.el-tree-node__children>.el-tree-node:before {
  left: 24px;
}

/deep/ .left-tree .tree-content>.el-tree-node>.el-tree-node__children>.el-tree-node .el-tree-node__children:before {
  left: 45px;
  top: 45px;
  height: calc(100% - 60px);
}

/* /deep/ .el-icon-delete{
  position: absolute;
  right: 0px;
} */
</style>